﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EasyErp.Domain;
using EasyErp.ViewModel;

namespace EasyErp.Controllers
{
    public class StyleController : Controller
    {
        private erpEntities db = new erpEntities();

        //
        // GET: /Style/

        public ViewResult Index()
        {
            return View(db.Style.ToList());
        }

        //
        // GET: /Style/Create

        public ActionResult Create()
        {
            var operationList = db.OperationInfo.Select(x => new OperationViewModel { OperationId = x.OperationId, OperationDesc = x.OperationDescription, IsCheecked = false }).ToList();

            StyleOperationViewModel styleOperation = new StyleOperationViewModel
            {
                OperationList = operationList
            };

            return View(styleOperation);
        }

        //
        // POST: /Style/Create

        [HttpPost]
        public ActionResult Create(StyleOperationViewModel styleOperations)
        {
            if (ModelState.IsValid)
            {
                db.Style.Add(styleOperations.Style);
                db.SaveChanges();

                int _styleId = styleOperations.Style.StyleId;

                // Assign Operation
                StyleOperation styleOperation;

                foreach (var item in styleOperations.OperationList)
                {
                    if (item.IsCheecked)
                    {
                        styleOperation = new StyleOperation
                        {
                            StyleId = _styleId,
                            OperationId = item.OperationId,
                        };

                        db.StyleOperation.Add(styleOperation);
                    }
                }

                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(styleOperations);
        }

        //
        // GET: /Style/Edit/5

        public ActionResult Edit(int id)
        {
            Style Style = db.Style.Find(id);

            var operationList = db.StyleOperation.Where(x => x.StyleId == id).Select(x => new OperationViewModel { StyleOperationId = x.StyleOperationId, OperationId = x.OperationId, OperationDesc = x.OperationInfo.OperationDescription, IsCheecked = true }).ToList();

            StyleOperationViewModel styleOperation = new StyleOperationViewModel
            {
                Style = Style,
                OperationList = operationList
            };

            return View(styleOperation);
        }

        //
        // POST: /Style/Edit/5

        [HttpPost]
        public ActionResult Edit(StyleOperationViewModel styleOperationViewModel)
        {
            if (ModelState.IsValid)
            {
                db.Entry(styleOperationViewModel.Style).State = EntityState.Modified;
                db.SaveChanges();

                // Assign Operation
                StyleOperation styleOperation;

                foreach (var item in styleOperationViewModel.OperationList)
                {
                    if (!item.IsCheecked)
                    {
                        styleOperation = new StyleOperation
                        {
                            StyleOperationId = item.StyleOperationId,
                            StyleId = styleOperationViewModel.Style.StyleId,
                            OperationId = item.OperationId,
                        };

                        db.StyleOperation.Attach(styleOperation);
                        db.StyleOperation.Remove(styleOperation);
                    }
                }

                db.SaveChanges();

                return RedirectToAction("Index");
            }
            return View(styleOperationViewModel);
        }

        //
        // GET: /Style/Delete/5

        public ActionResult Delete(int id)
        {
            Style Style = db.Style.Find(id);
            return View(Style);
        }

        //
        // POST: /Style/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Style Style = db.Style.Find(id);
            db.Style.Remove(Style);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult TimeTable(int id)
        {
            List<TimeTable> timeTable = db.TimeTable.ToList();

            if (timeTable.Count != 0)
            {
                return RedirectToAction("Edit", "TimeTable", new { id = id });
            }
            else
            {
                return RedirectToAction("Create", "TimeTable", new { id = id });
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}